import hmac
import hashlib
import base64
from typing import List
import urllib.parse
import requests, json
import time

ksec = "SECbfdfc87d0cf73687239e60f5fe40acd77c499915b677d1c5e6e732c2b9ec7a6b"
kaccessToken = "f65d24a78232dadc4db4d3a340d2f47e8cba34bc308f1dbde2112cef876fd34b"

class DingTalk():

     def sendInfo(self, info):
         dingTalkApi = self.getSignUrl()
         headers = {
             "Content-Type": "application/json; charset=utf-8"
         }
         atMobiles = []
         sendContent = self.parsePgyInfo(info)
         print(sendContent)
         data = {
             'msgtype': "text",
             'text': {
                 'content':sendContent
             },
             'at': {
                'atMobiles': atMobiles,
                 'isAtAll': "true"
             }
         }
         jsonData = json.dumps(data)
         print(jsonData)
         r = requests.post(dingTalkApi, data = jsonData, headers = headers)
         print(r.json())

     def sendInfoWithAppStore(self,info):
         dingTalkApi = self.getSignUrl()
         headers = {
             "Content-Type": "application/json; charset=utf-8"
         }
         atMobiles = []
         sendContent = self.getAppStoreMsg(info)
         data = {
             'msgtype': "text",
             'text': {
                 'content': sendContent
             },
             'at': {
                'atMobiles': atMobiles,
                'isAtAll': "true"
             }
         }
         jsonData = json.dumps(data)
         r = requests.post(dingTalkApi, data = jsonData, headers = headers)
         print(r.json())

     def getSignUrl(self):
         timestamp = str(round(time.time() * 1000))
         secret = ksec
         secret_enc = secret.encode('utf-8')
         string_to_sign = '{}\n{}'.format(timestamp, secret)
         string_to_sign_enc = string_to_sign.encode('utf-8')
         hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
         sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
         return "https://oapi.dingtalk.com/robot/send?access_token=%s&timestamp=%s&sign=%s" % (kaccessToken, timestamp, sign)

     def parsePgyInfo(self, dataDic):
         buildName = "应用名：" + dataDic['buildName']
         buildVersion = "版本号：" + dataDic['buildVersion']
         buildVersionNo = "build号：" + dataDic['buildVersionNo']
         buildShortcutUrl = "下载链接：%s" % (dataDic['download_url'])
         buildUpdateDescription = "更新日志：\n%s" % (dataDic['buildUpdateDescription'])
         return "%s\n%s\n%s\n%s\n%s\n" % (buildName, buildVersion, buildVersionNo,buildShortcutUrl,buildUpdateDescription)
         
     def getAppStoreMsg(self, info):
         buildName = "应用名：" + info['buildName']
         buildVersion = "版本号：" + info['buildVersion']
         buildVersionNo = "build号：" + info['buildVersionNo']
         desc = "AppStore 上传成功，15 分钟之后可以构建版本"
         return "%s\n%s\n%s\n%s\n" % (buildName, buildVersion, buildVersionNo,desc)             


